package com.dyp.cachedemo.service;

import com.dyp.cachedemo.service.dto.PersonDTO;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
public class PersonService {

    @Cacheable(cacheNames = "HelloWorldCache")
    public PersonDTO getPersonById(String id){
        PersonDTO personDTO = new PersonDTO();
        personDTO.setId("id");
        personDTO.setName("test name");
        System.out.println("get from db");
        return personDTO;
    }

    @CachePut(cacheNames = "person", key="#personDTO.id")
    public PersonDTO updatePerson(PersonDTO personDTO)
    {
        System.out.println("update cache");
        return personDTO;
    }
    @CacheEvict(cacheNames = "person")
    public void deletePerson(String id){
        System.out.println("delete from cache");
    }
}
